layui.use(["element", "layer", "table", "form"], function() {
	var $ = layui.jquery;
	var layer = layui.layer;
	var table = layui.table;
	var sessionStorage = window.sessionStorage;
	var userId = sessionStorage.getItem("userId");
	var userName = sessionStorage.getItem("userName");
	var ip = "http://localhost"; //后台ip
	var port = ":9014"; //端口

	function convertObj(data) {
		var _result = [];
		for(var key in data) {
			var value = data[key];
			if(value.constructor == Array) {
				value.forEach(function(_value) {
					_result.push(key + "=" + _value);
				});
			} else {
				_result.push(key + '=' + value);
			}
		}
		return _result.join('&');
	}

	$(".layui-btn").click(function() {
		var collection = $(".layui-btn");
		$.each(collection, function() {
			$(this).addClass("layui-btn-primary");
			$(this).removeClass("layui-btn-normal");
		})
		$(this).removeClass("layui-btn-primary");
		$(this).addClass("layui-btn-normal");
		var name = $(this).attr("id");
		var param = {};
		param["belongProduct"] = $("select").val();
		switch(name) {
			case "all":
				break;
			case "state1":
				param["state"] = 1;
				break;
			case "state4":
				param["state"] = 4;
				break;
			case "state5":
				param["state"] = 5;
				break;
			case "state6":
				param["state"] = 6;
				break;
			default:
				param[name] = userId;
		}
		table.render({
			method: "get",
			url: ip + port + '/demand/searchByPage',
			elem: '#table',
			height: 450,
			where: param,
			page: true,
			skin: 'nob',
			cols: [
				[{
						field: "id",
						title: "ID",
						width: 30
					},
					{
						field: "priority",
						title: "优先级",
						width: 80,
						templet: function(d) {
							if(d.priority == 1) {
								return '<div style="color:red;">紧急</div>';
							} else if(d.priority == 2) {
								return '<div style="color:orange;">严重</div>';
							} else if(d.priority == 3) {
								return '<div style="color:yellow;">重视</div>';
							} else if(d.priority == 4) {
								return '<div style="color:blue;">常规</div>';
							} else if(d.priority == 0) {
								return '<div>未定义</div>';
							}
						}
					},
					{
						field: "belongProductName",
						title: "所属产品",
						width: 130
					},
					{
						field: "name",
						title: "需求名称",
						width: 450,
						style: "cursor:pointer;color:blue"
					},
					{
						field: "creatorName",
						title: "创建者",
						width: 80
					},
					{
						field: "pointerName",
						title: "指派者",
						width: 80
					},
					{
						field: "approverName",
						title: "审批者",
						width: 80
					},
					{
						field: "spendTime",
						title: "已消耗时间(h)",
						width: 100
					},
					{
						field: "state",
						title: "状态",
						width: 70,
						templet: function(d) {
							if(d.state == 1) {
								return '<div>待激活</div>';
							} else if(d.state == 2) {
								return '<div style="color:red;">进行中</div>';
							} else if(d.state == 3) {
								return '<div>已暂停</div>';
							} else if(d.state == 4) {
								return '<div>已完成</div>';
							} else if(d.state == 5) {
								return '<div>已关闭</div>';
							} else if(d.state == 6) {
								return '<div>已审批</div>';
							}
						}
					},
					{
						field: "do",
						title: "操作",
						width: 150,
						toolbar: "#barDemo"
					}
				]
			],
			parseData: function(res) {
				return {
					"code": 0,
					"count": res.records,
					"data": res.data
				}
			}
		});
		table.on('tool(table)', function(obj) {
			switch(obj.event) {
				case 'approve':
					if(obj.data.state == 2) {
						layer.msg("该" + obj.data.objType + "已启动,不可操作!(需暂停)");
					} else if(obj.data.state == 4) {
						layer.msg("该" + obj.data.objType + "已完成,不可操作!");
					} else if(obj.data.state == 5) {
						layer.msg("该" + obj.data.objType + "已关闭,不可操作!");
					} else {
						var uurl = encodeURI("../little/approvePage.html?" + convertObj(obj.data));
						var index = layer.open({
							type: 2,
							title: "需求审批",
							area: ['70%', '80%'],
							fix: false,
							maxmin: true,
							shadeClose: true,
							shade: 0.4,
							skin: 'layui-layer-rim',
							content: [uurl, "no"],
						});
					}
					break;
				case 'close':
				if(obj.data.state == 1){
					layer.msg("该"+obj.data.objType+"未开始,不可操作!");
				}else if(obj.data.state == 2){
					layer.msg("该"+obj.data.objType+"正在进行中,不可操作!");
				}else if(obj.data.state == 3){
					layer.msg("该"+obj.data.objType+"已暂停,不可操作!");
				}else if(obj.data.state == 5){
					layer.msg("该"+obj.data.objType+"已关闭,不可操作!");
				}else{
					layer.confirm('您确定关闭该' + obj.data.objType + '？', {
						btn: ['确定'] //按钮
					}, function() {
						getdata("/demand/update", {
							id: obj.data.id,
							closer: userId,
							state: 5,
							loginUserName: userName,
							loginUserId: userId
						}, "POST", function(data) {
							console.log(data);
							if(data.success) {
								layer.msg('已成功关闭', {
									icon: 1
								},function (){
									parent.location.reload();
								});
							} else {
								layer.msg(data.msg);
							}
						});
					});
				}
					break;
				case 'editor':
					var uurl = encodeURI("../little/editPage.html?" + convertObj(obj.data));
					var index = layer.open({
						type: 2,
						title: "完成",
						area: ['90%', '90%'],
						fix: false,
						maxmin: true,
						shadeClose: true,
						shade: 0.4,
						skin: 'layui-layer-rim',
						content: [uurl, "no"],
					});
					break;
			}
		});

	})
	$("#all").click();
})